#!/usr/bin/env perl
# Finds backups, and writes them to tape. 

use File::stat;
use Time::localtime;
use Data::Dumper;

my $configfile = "/backups/tape.conf.pl";
my @unqhosts = ();
my @backupfiles = ();
my %fullbackups = ();
my %lastfullbackups = ();
my %incbackups = ();
my %reqfullbackups = ();
my $debug = 0;

eval('require("$configfile")');
die "*** Failed to eval() file $configfile:\n$@\n" if ($@);

sub filename2hostname {
	my $tmp = $_;
	$tmp =~ s/slot\d+\///; 
	$tmp =~ s/\.\d$//;
	$tmp =~ s/\/\d{5}\./\//;
	return $tmp;
}

sub identifyhosts {
	my %tmphash = ();
	foreach(@backupfiles) {
		my $tmp = $_;
		$tmp = filename2hostname($tmp);
		$tmphash{$tmp} = 1;
	}
	foreach(keys %tmphash) { push (@unqhosts, $_ ) if $_ ne ''; }

	if ($debug) {
		foreach(@unqhosts) {
			print "[ DEBUG ] Found: $_\n";
		}
	}
}

sub findfiles {
	foreach (@backupsets) {
		open(FINDCMD, "find $_ -type f  |");
		while(<FINDCMD>) {
			chomp;
			next if ($_ !~ /\.\d+$/); 
			print "[ DEBUG ] Found file: $_\n" if $debug;
			push (@backupfiles, $_);
		}
		close(FINDCMD);
	}
}

sub searchfullbackups {
	foreach(@backupfiles) {
		next unless ($_ =~ /\.0$/); 
		my $unqhost = filename2hostname $_;
		push @{ $fullbackups{$unqhost} }, $_; 
		print "[ HOST: $unqhost ] | [ FULL Backup: $_ ]\n" if $debug;
	}
}

sub searchincbackups {
	foreach(@backupfiles) {
		next if ($_ =~ /\.0$/); 
		my $unqhost = filename2hostname $_;
		push @{ $incbackups{$unqhost} }, $_;
		print "[ HOST: $unqhost ] | [ INCR Backup: $_ ]\n" if $debug;
        }
}

sub cleanfullbackuplist {
	foreach $unqhost (@unqhosts) {
		my %unsortedmtime;
		my @sortedmtime;
		foreach( @{$fullbackups{$unqhost}} ) {
			chomp ($_);
			my $timestamp = (stat($_))->mtime;
			$unsortedmtime{$_}=$timestamp;
		}

	    	foreach $key (sort { $unsortedmtime{$b} <=> $unsortedmtime{$a} } keys %unsortedmtime) {
			push(@sortedmtime, $key);
    		}

		next if @sortedmtime == 0;
		$lastfullbackups{$unqhost} = $sortedmtime[0];

	}
}

sub cleanincbackuplist {
	foreach $unqhost (keys %lastfullbackups) {
		next if (@{$incbackups{$unqhost}}) == undef;
		my $i = $incbackups{$unqhost};
		my $file = $lastfullbackups{$unqhost};
		my $fulltimestamp = (stat($file)->mtime);
		foreach ( @{ @incbackups{$unqhost} } ) {
			chomp ($_);
			my $timestamp = (stat($_))->mtime;
			push (@{$reqincbackups{$unqhost}}, $_) if ($timestamp > $fulltimestamp) 
		}
	}
}

sub printbackups {
	foreach $unqhost (@unqhosts) {
		print STDERR "#######################################################\n";
		print STDERR " [ Backup Set ] : [ $unqhost ] \n";
		print STDERR " [ Last full backup ] : [ $lastfullbackups{$unqhost} ] \n";
		print "$lastfullbackups{$unqhost}\n";

		if ((@{@reqincbackups{$unqhost}}) != undef) {
			foreach (@{@reqincbackups{$unqhost}}) {
				print STDERR " [ Inc Backup ] : [ $_ ] \n";
				print "$_\n";
			}
		}
	}
}

sub finalizelist {
	my $bla = "";
	foreach $unqhost (keys %lastfullbackups) {
	        $bla = $bla . " " . $lastfullbackups{$unqhost};
	        if ((@{@reqincbackups{$unqhost}}) != undef) {
	       		$bla = $bla . " " . join(' ', @{$reqincbackups{$unqhost}});
	        }
	}
	return $bla;
}

sub write_to_tape {
	open(STATUS, "/usr/sbin/mtx status|");
	while(<STATUS>) {
		print;
	}
	close(STATUS);
	

	my $list = finalizelist;
	
	open(TAR, "$tarcmd $list|");
	while(<TAR>) {
		print;
	}
	close(TAR);
		
}


findfiles();
identifyhosts();
searchfullbackups();
searchincbackups();
cleanfullbackuplist();
cleanincbackuplist();
printbackups();

my $d = `date`; chomp($d); print "$d: Writing to tape.\n";
write_to_tape(); 
my $d = `date`; chomp($d); print "$d: Done writing to tape.\n";


